#!/usr/bin/sh
CYGWIN=nontsec; export CYGWIN
# /////////////////////////////////////////////////////////////////////////////////////
# /////////////////////////////////////////////////////////////////////////////////////
# Estas rutas deben modificarse por las apropiadas en cada PC de desarrollo
# /////////////////////////////////////////////////////////////////////////////////////
# Ruta de desarrollo
APPLICAST_SOURCE_BASE=d:/code/applicast/widget-sandbox
# Copia
APPLICAST_BUILD_BASE=$APPLICAST_SOURCE_BASE'/release'
# Directorio del emulador
APPLICAST_EXPORT_BASE=c:/Users/estanis.INSON/Documents/My\ Applicast
# /////////////////////////////////////////////////////////////////////////////////////
# /////////////////////////////////////////////////////////////////////////////////////

# check parameter
if [ -z "$1" ]; then
  echo 'Please specify an applicast project folder!';
  exit 0
fi

if [ ! -d './'"$1" ]; then
  echo 'Specified applicast project folder does not exist!'
  echo "$1"
  exit 0
fi

BUILD_RDIR=$APPLICAST_BUILD_BASE'/'$1
EXPORT_RDIR="$APPLICAST_EXPORT_BASE"'/'$1

echo
echo "Applicast(tm) project: $1"

# checking release directories
echo 'Checking release directory(ies)...'
if [ ! -d $APPLICAST_BUILD_BASE ]; then
  mkdir $APPLICAST_BUILD_BASE;
fi
rm -fR $BUILD_RDIR
mkdir $BUILD_RDIR

# build
echo 'Building...'

# perform a local svn export (TODO svn export)
cp -fR ./"$1"/* $BUILD_RDIR
find $BUILD_RDIR/ -depth -type d -name .svn -exec rm -rf {} \;

if [ -e $BUILD_RDIR/core.js ]; then
  # sprockets
  sprocketize -C $BUILD_RDIR -I ../../lib -I ./ -a assets/ $BUILD_RDIR/core.js > $BUILD_RDIR/widget.full.js
  
  # yui compressor
  java -jar yuicompressor-2.4.2.jar --charset utf-8 -o $BUILD_RDIR/widget.js $BUILD_RDIR/widget.full.js
  
  # check for full-screen widget code
  if [ -e $BUILD_RDIR/core_fullscreen.js ]; then
    # sprockets
    sprocketize -C $BUILD_RDIR -I ../../lib -I ./ -a assets/ $BUILD_RDIR/core_fullscreen.js > $BUILD_RDIR/widget_fullscreen.full.js
    # yui compressor
    java -jar yuicompressor-2.4.2.jar --charset utf-8 -o $BUILD_RDIR/widget_fullscreen.js $BUILD_RDIR/widget_fullscreen.full.js
  fi
fi

# cleanup
echo 'Cleaning up...'
mv -f $BUILD_RDIR/widget.js $BUILD_RDIR/widget.bak
if [ -e $BUILD_RDIR/widget_fullscreen.js ]; then
  mv -f $BUILD_RDIR/widget_fullscreen.js $BUILD_RDIR/widget_fullscreen.bak
fi
rm -f $BUILD_RDIR/*.js
mv -f $BUILD_RDIR/widget.bak $BUILD_RDIR/widget.js
if [ -e $BUILD_RDIR/widget_fullscreen.bak ]; then
  mv -f $BUILD_RDIR/widget_fullscreen.bak $BUILD_RDIR/widget_fullscreen.js
fi
rm -f $BUILD_RDIR/build.sh

# export
echo 'Exporting release build...'
rm -fR "$EXPORT_RDIR"
mkdir "$EXPORT_RDIR"
cp -fR $BUILD_RDIR/* "$EXPORT_RDIR"

# bye
echo 'Job done! Bye!'